package com.bicntech.admin.controller.airport;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bicntech.common.annotation.RepeatSubmit;
import com.bicntech.common.core.controller.SuperController;
import com.bicntech.common.core.domain.R;
import com.bicntech.system.dto.InfieldBasicDataQueryDTO;
import com.bicntech.system.dto.InfieldInspectAddDTO;
import com.bicntech.system.dto.InfieldRefuelingVolumeDTO;
import com.bicntech.system.dto.InfieldVehicleEquipmentInspectDTO;
import com.bicntech.system.service.InfieldVehicleEquipmentInspectService;
import com.bicntech.system.service.VehicleRefuelingStatisticsService;
import com.bicntech.system.vo.VehicleRefuelingStatisticsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.List;

@RequestMapping("/infield-vehicle-equipment-inspect")
@RestController
@RequiredArgsConstructor
public class InfieldVehicleEquipmentInspectController extends SuperController {

    private final InfieldVehicleEquipmentInspectService infieldVehicleEquipmentInspectService;


    private final VehicleRefuelingStatisticsService vehicleRefuelingStatisticsService;

    @PostMapping("/listByPage")
    public R<IPage<InfieldVehicleEquipmentInspectDTO>> listBypage(@RequestBody InfieldBasicDataQueryDTO infieldBasicDataQueryDTO) {
        var page = infieldVehicleEquipmentInspectService.listInfieldVehicleEquipmentInspectsByPage(packPage(), infieldBasicDataQueryDTO);
        return R.ok(page);
    }

    /**
     * 今日已完成检查多少台特种车辆
     */
    @GetMapping(value = "/getInfieldSpecialVehicleCount")
    public R<?> getInfieldSpecialVehicleCount() {
        return R.ok(infieldVehicleEquipmentInspectService.getInfieldSpecialVehicleCount());
    }

    @PostMapping(value = "/listNoPage")
    public R<List<InfieldVehicleEquipmentInspectDTO>> listNoPage(@RequestBody InfieldVehicleEquipmentInspectDTO infieldVehicleEquipmentInspectDTO) {
        List<InfieldVehicleEquipmentInspectDTO> infieldVehicleEquipmentInspectList = infieldVehicleEquipmentInspectService.listInfieldVehicleEquipmentInspects(infieldVehicleEquipmentInspectDTO);
        return R.ok(infieldVehicleEquipmentInspectList);
    }

    /**
     * 查询车辆今日是否已经巡检
     */
    @GetMapping(value = "/checkByWagonNumber")
    public R<Boolean> checkByWagonNumber(
            @RequestParam("wagonNumber") String wagonNumber,
            @RequestParam("localDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate localDate) {
        return R.ok(infieldVehicleEquipmentInspectService.checkByWagonNumber(wagonNumber, localDate));
    }

    @GetMapping(value = "/queryById")
    public R<InfieldVehicleEquipmentInspectDTO> queryById(@RequestParam(name = "id", required = true) Long id) {
        InfieldVehicleEquipmentInspectDTO infieldVehicleEquipmentInspectDTO = infieldVehicleEquipmentInspectService.getInfieldVehicleEquipmentInspectById(id);
        return R.ok(infieldVehicleEquipmentInspectDTO);
    }

    @PostMapping(value = "/add")
    @RepeatSubmit(message = "不允许重复提交，请稍候再试")
    public R<?> addInfieldVehicleEquipmentInspect(@RequestBody @Validated InfieldInspectAddDTO addDTO) {
        return R.ok(infieldVehicleEquipmentInspectService.addInfieldVehicleEquipmentInspect(addDTO));
    }


    /**
     * 编辑
     */
    @PutMapping(value = "/update")
    public R<?> update(@RequestBody @Validated InfieldRefuelingVolumeDTO infieldRefuelingVolumeDTO) {
        infieldVehicleEquipmentInspectService.updateInspect(infieldRefuelingVolumeDTO);
        return R.ok("编辑成功!");
    }


    /**
     * 通过id删除
     */
    @DeleteMapping(value = "/delete/{id}")
    @RepeatSubmit(message = "不允许重复提交，请稍候再试")
    public R<?> delete(@PathVariable Long id) {
        infieldVehicleEquipmentInspectService.deleteInfieldVehicleEquipmentInspectById(id);
        return R.ok("删除成功!");
    }

    /**
     * 获取年加油量统计
     */
    @GetMapping(value = "/annualStatistics")
    public R<List<VehicleRefuelingStatisticsVO>> annualStatistics(
            @NotNull(message = "年份不能为空")
            @RequestParam("year") Integer year,
            @RequestParam(value = "wagonNumber", required = false) String wagonNumber,
            @RequestParam(value = "checklistType", required = false) Integer checklistType) {
        return R.ok(vehicleRefuelingStatisticsService.annualStatistics(year, wagonNumber, checklistType));
    }

}